﻿using System;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using NTTDATA.BEENChina.CaseMonitor.Base;

namespace NTTDATA.BEENChina.CaseMonitor.UI
{


    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
           

        }

        private Monitor monitor;  

        private bool isRun = false; 
        

        private void toolStripButtonStar_Click(object sender, EventArgs e)
        {
            ((ToolStripButton)sender).Checked = !((ToolStripButton)sender).Checked;
            if (!isRun)
            {
                isRun = true;

                monitor = new Monitor(this);
                monitor.IsCall = true;
                Star();

            }
            else
            {
                isRun = false;
                Stop();


            }
        }

        private void Star()
        {
            this.toolStripButtonStar.Image = Properties.Resources.menuStop;
            monitor.Start();
        }

        private void Stop()
        {
            this.toolStripButtonStar.Image = Properties.Resources.menuRun;
            monitor.Stop();
        } 

        private void ClearMonitor()
        {
            this.listViewMonitor.Clear();
        }

        public void ClearMessage()
        {
            listViewMonitor.Items.Clear();
            
        }
       
        public void showMessage(String serverName,String monitorType,String result,bool call =false)
        {
            //drop last monitor
            string itemKey = string.Format("{0}-{1}", serverName, monitorType);

            //每次监视前 上次监视记录清除，此处逻辑可以省略
            listViewMonitor.Items.RemoveByKey( itemKey);

            ListViewItem item = new ListViewItem();
            item.Text = serverName;
            item.Name = itemKey;
            

            item.SubItems.AddRange(new string[]{  
                //serverName, 
               System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                result,
                call?"●":""});

            //item.ImageIndex = 0;

            //this.listViewMonitor.Items.Add(item);
            this.listViewMonitor.Items.Insert(0,item);
            
           Application.DoEvents();

        }

        FormConfig formConfig = new FormConfig();

        private void toolStripButtonConfig_Click(object sender, EventArgs e)
        {

            if (isRun)
            {
                MessageBox.Show(@"停止监视再修改");
            }
            else
            {


                // ((ToolStripButton)sender).Checked = !((ToolStripButton)sender).Checked;
                if (formConfig == null || formConfig.IsDisposed)
                {
                    formConfig = new FormConfig();
                }


                formConfig.StartPosition = FormStartPosition.CenterParent;
                formConfig.ShowDialog();
            }
        }

        private void toolStripButtonTel_Click(object sender, EventArgs e)
        {
            if (this.monitor == null)
                return;

            ((ToolStripButton) sender).Checked = !((ToolStripButton) sender).Checked;

            this.monitor.IsCall = ((ToolStripButton)sender).Checked;

            if (((ToolStripButton)sender).Checked)
            {
                toolStripButtonTel.Image = Properties.Resources.phone;
            }
            else
            {
                toolStripButtonTel.Image = Properties.Resources.DialHS;
            }
        }

        public FormMsg MsgFrom = new FormMsg();

        public bool IsMonitor { get; set; }

        private void toolStripButtonMonitor_Click(object sender, EventArgs e)
        {
            bool cheked = !((ToolStripButton)sender).Checked;
            ((ToolStripButton) sender).Checked = cheked;

            if (cheked)
            {
                if (MsgFrom == null || MsgFrom.IsDisposed)
                {
                    MsgFrom = new FormMsg();
                }

                IsMonitor = true;
                
                MsgFrom.StartPosition = FormStartPosition.CenterParent;
                MsgFrom.Show();
            }
            else
            {
                IsMonitor = false;
                MsgFrom.Hide();
                //formMsg.Close();
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            ClearMessage();
        }

        private FormMonitorQuery monitorQuery;
        private void toolStripButtonMsg_Click(object sender, EventArgs e)
        {
            if (monitorQuery == null || monitorQuery.IsDisposed)
            {
                monitorQuery = new FormMonitorQuery();
            }

            monitorQuery.Show();

        }



    }



}
